package com.peng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.peng.pojo.Order;
import com.peng.mapper.OrderMapper;
import com.peng.pojo.SeckillGoods;
import com.peng.pojo.SeckillOrder;
import com.peng.pojo.User;
import com.peng.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peng.service.SeckillGoodsService;
import com.peng.service.SeckillOrderService;
import com.peng.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Dengp
 * @since 2022-07-21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private SeckillOrderService seckillOrderService;
    @Autowired
    private SeckillGoodsService seckillGoodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Order seckill(User user, GoodsVo seckillGoods) {
//        LambdaQueryWrapper<SeckillGoods> queryWrapper = new LambdaQueryWrapper<SeckillGoods>().eq(SeckillGoods::getGoodsId, seckillGoods.getId());
//        SeckillGoods goods = seckillGoodsService.getOne(queryWrapper);
//        goods.setStockCount(goods.getStockCount()-1);
//        seckillGoodsService.updateById(goods);
        ValueOperations valueOperations = redisTemplate.opsForValue();

        SeckillGoods goods = seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>().eq("goods_id", seckillGoods.getId()));
        seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);
        boolean seckillGoodsResult = seckillGoodsService.update(new UpdateWrapper<SeckillGoods>()
                        .setSql("stock_count = " + "stock_count-1")
                        .eq("goods_id", seckillGoods.getId())
                        .gt("stock_count", 0));
        if (seckillGoods.getStockCount() < 1) {
            //判断是否还有库存
            valueOperations.set("isStockEmpty:" + seckillGoods.getId(), "0");
            return null;
        }
        Order order = new Order();
        order.setUserId(user.getId());
        order.setGoodsId(goods.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsName(seckillGoods.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(seckillGoods.getSeckillPrice());
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(new Date());
        orderService.save(order);
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setUserId(user.getId());
        seckillOrder.setGoodsId(goods.getId());
        seckillOrderService.save(seckillOrder);
        redisTemplate.opsForValue().set("order:" + user.getId() + ":" + seckillGoods.getId(), seckillOrder);
        return order;
    }
}
